import Vue from 'vue'
import Router from 'vue-router'
import StudentIndex from '../components/student/StudentIndex'
import Login from '../components/Login'
import TeacherIndex from '../components/teacher/TeacherIndex'

Vue.use(Router)
// 解决报错 Uncaught (in promise) Error: Navigation cancelled
const originalPush = Router.prototype.push
const originalReplace = Router.prototype.replace
// push
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
Router.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Default',
      redirect: '/login',
      component: Login
    },
    {
      path: '/student/',
      name: 'Student',
      component: StudentIndex,
      children: [
        {
          path: '/student/booking',
          name: 'StudentBooking',
          component: () => import('../components/student/content/StudentBooking'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/student/process',
          name: 'StudentProcess',
          component: () => import('../components/student/content/StudentProcess'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/student/user',
          name: 'StudentCenter',
          component: () => import('../components/student/content/StudentCenter'),
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/Teacher',
      name: 'Teacher',
      component: TeacherIndex,
      children: [
        {
          path: '/teacher/index',
          name: 'TeacherIndex',
          component: () => import('../components/teacher/TeacherIndex'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/teacher/check',
          name: 'TeacherCheck',
          component: () => import('../components/teacher/content/TeacherCheck'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/teacher/notify',
          name: 'TeacherNotify',
          component: () => import('../components/teacher/content/TeacherNotify'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/teacher/user',
          name: 'TeacherCenter',
          component: () => import('../components/teacher/content/TeacherCenter'),
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/admin',
      name: 'Admin',
      component: () => import('../components/admin/AdminIndex'),
      meta: {
        requireAuth: true
      },
      children: [
        {
          path: '/admin/index',
          name: 'AdminIndex',
          component: () => import('../components/admin/AdminIndex'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/student_manager',
          name: 'StudentManager',
          component: () => import('../components/admin/content/StudentManager'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/teacher_manager',
          name: 'TeacherManager',
          component: () => import('../components/admin/content/TeacherManager'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/blacklist_manager',
          name: 'BlacklistManager',
          component: () => import('../components/admin/content/BlacklistManager'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/notify_manager/discord',
          name: 'NotifyDiscord',
          component: () => import('../components/admin/content/NotifyDiscord'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/notify_manager/table',
          name: 'NotifyTable',
          component: () => import('../components/admin/content/NotifyTable'),
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../components/Login')
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('../components/Register')
    },
    {
      path: '/*',
      name: 'error',
      component: () => import('../components/pages/Error404')
    }
  ]
})

// 用于创建默认路由
export const createRouter = routes => new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Default',
      redirect: '/login',
      component: Login
    },
    {
      path: '/student/',
      name: 'Student',
      component: StudentIndex,
      children: [
        {
          path: '/student/booking',
          name: 'StudentBooking',
          component: () => import('../components/student/content/StudentBooking'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/student/process',
          name: 'StudentProcess',
          component: () => import('../components/student/content/StudentProcess'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/student/user',
          name: 'StudentCenter',
          component: () => import('../components/student/content/StudentCenter'),
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/Teacher',
      name: 'Teacher',
      component: TeacherIndex,
      children: [
        {
          path: '/teacher/index',
          name: 'TeacherIndex',
          component: () => import('../components/teacher/TeacherIndex'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/teacher/check',
          name: 'TeacherCheck',
          component: () => import('../components/teacher/content/TeacherCheck'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/teacher/notify',
          name: 'TeacherNotify',
          component: () => import('../components/teacher/content/TeacherNotify'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/teacher/user',
          name: 'TeacherCenter',
          component: () => import('../components/teacher/content/TeacherCenter'),
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/admin',
      name: 'Admin',
      component: () => import('../components/admin/AdminIndex'),
      meta: {
        requireAuth: true
      },
      children: [
        {
          path: '/admin/index',
          name: 'AdminIndex',
          component: () => import('../components/admin/AdminIndex'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/student_manager',
          name: 'StudentManager',
          component: () => import('../components/admin/content/StudentManager'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/teacher_manager',
          name: 'TeacherManager',
          component: () => import('../components/admin/content/TeacherManager'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/blacklist_manager',
          name: 'BlacklistManager',
          component: () => import('../components/admin/content/BlacklistManager'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/notify_manager/discord',
          name: 'NotifyDiscord',
          component: () => import('../components/admin/content/NotifyDiscord'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/admin/notify_manager/table',
          name: 'NotifyTable',
          component: () => import('../components/admin/content/NotifyTable'),
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../components/Login')
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('../components/Register')
    },
    {
      path: '/*',
      name: 'error',
      component: () => import('../components/pages/Error404')
    }
  ]
})
